/**
 * kl yy-ds @2021
 */
package com.cxps.server.thread.service;

import com.cxps.cloud.utils.IdUtils;
import com.cxps.server.product.domain.ProductEntity;
import com.cxps.server.thread.mapper.ThreadProductMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO(类描述)
 *
 * @author YouCai.Liu
 * @since 2021/11/30
 * ...
 */
@Slf4j
public class ThreadService2 {

    private ThreadProductMapper threadProductMapper;
    private int threadSize;
    private int increment = 0;
    public boolean flag = false;

    public void run() {
        List<ProductEntity> list = new ArrayList<>();
        synchronized (ThreadService2.class){
            for (int i = 0; i < threadSize; i++) {
                increment++;
                ProductEntity entity = new ProductEntity();
                entity.setId(IdUtils.generateId());
                entity.setProductName("这是单线程插入的商品数据" + increment);
                entity.setProductTitle("这是单线程插入的商品标题数据" + increment);
                entity.setProductCode(IdUtils.generateId());
                entity.setProductType(IdUtils.generateId());
                entity.setChannel(increment);
                entity.setSaleType(increment);
                entity.setUnit("包斤袋");
                entity.setProductImg(IdUtils.generateId() + ".png");
                entity.setProductPrice(Double.parseDouble(increment + ""));
                entity.setSaleNum(increment);
                entity.preInsert();
                entity.setIsUp(1);
                if (list.size() < 500) {
                    list.add(entity);
                }
                if (list.size() == 500) {
                    threadProductMapper.batchInsert(list);
                    list.clear();
                }
            }
        }
        if (list.size() > 0) {
            threadProductMapper.batchInsert(list);
            list.clear();
        }

    }

    public int getThreadSize() {
        return threadSize;
    }

    public void setThreadSize(int threadSize) {
        this.threadSize = threadSize;
    }

    public ThreadProductMapper getThreadProductMapper() {
        return threadProductMapper;
    }

    public void setThreadProductMapper(ThreadProductMapper threadProductMapper) {
        this.threadProductMapper = threadProductMapper;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
